//返回值为需不需要关闭窗口
function yesOperator() {
    var retValue = addData();
    return retValue;
}

function updateLoad() {
    var params = $$.getQueryString();
    $$.ajax({
        url: "/rest/IotCalcBusiness/query",
        data: {
            id: params.rowId
        },
        success: function (data) {
            $("#b_id").val(data.id);
            $("#b_ttable").val(data.ttable);
            $("#b_tname").val(data.tname);
            $("#b_tcode").val(data.tcode);
            $("#b_tcodeOld").val(data.tcode);

            $("#b_stable").val(data.stable);
            $("#b_calcExpr").val(data.calcExpr);
            $("#b_scode").val(data.scode);
            $("#b_scolumn").val(data.scolumn);
            $("#b_tcolumn").val(data.tcolumn);
            $("#b_calcDate").val(data.calcDate);

            $("#b_calcStart").val(data.calcStart);

            $("#b_calcEnd").val(data.calcEnd);
            $("#b_scale").val(data.scale);
            $("#b_status").val(data.status);

            $("#b_rowSort").val(data.rowSort);

            $("#b_remarks").val(data.remarks);
            $("#b_rptCode").val(data.rptCode);

            var status = data.status;

            $("input[name^='b_status_']").each(function () {

                if ($(this).val() == status) {

                    this.checked = 'checked';

                }

            });


            $("#b_calcfCodeOld").val(data.calcfCode);


        }
    });


}

function validForm() {

    return   $('#theform').valid();


    /*
    //判断编码唯一性
    if(!notNull("b_calcfCode","反向计算编码")){
        return false;
    }
    var calcfCodeOld=$("#b_calcfCodeOld").val();
    var calcfCode=$("#b_calcfCode").val();
    if(calcfCodeOld==calcfCode){

    }else{

            var args={};
            args.c_calcfCode_1=$("#b_calcfCode").val();
            args.page=0;
            args.pageSize=5;
            args.isPage=1;


            var exists;
            $$.ajax({
                url: "/bootstrap/basegrid/IotCalcBusiness/list",
                async:false,
                data: args,
                success: function (data) {
                    if(data.length && data.length>0){
                        exists=true;
                     }else{
                        exists=false;
                    }
                }
            });
            if(exists){
               $$.alert("反向计算编码已经存在,请重新输入!",function(index){
                   layer.close(index);
                   $("#b_calcfCode").focus();
               });
               return false;
             }

    }
    */

}

function addData() {
    var param = {};
    var validFormRet;
    $$.form({
        async: false,
        able: function () {
            validFormRet = false;
        },
        checkForm: validForm,
        success: function () {
            validFormRet = true;
            returnHome();
        }
    });
    return validFormRet;
}

function forwardCalcValid() {
    $("#theform").validate({
        rules: {
            b_ttable: {
                required: true,
                minlength: 1,
                number: false
            },
            b_tcode: {
                required: true,
                minlength: 1,
                number: false,
                isCode: true
            },
            b_tcolumn: {
                required: true,
                minlength: 1
            },
            b_scolumn: {
                required: true,
                minlength: 1,
            },
            b_calcDate: {
                required: true,
                minlength: 1,
                isRptDatetimeFormula:true

            },
            b_stable: {
                required: true,
                minlength: 1,
                number: false
            },
            b_scode: {
                required: true,
                minlength: 1,
                number: false
            },
            b_calcExpr:{
                required: true,
                isRptDataFormula:true
            },
            b_calcStart: {
                required: true,
                minlength: 1,
                number: false,
                isRptDatetimeFormula:true
            },
            b_calcEnd:{
                required: false,
                isRptDatetimeFormula:true
            },
            b_rowSort: {
                required: true,
                minlength: 1,
                isPositiveInteger: true
            },
            b_scale: {
                required: true,
                minlength: 1,
                isPositiveInteger: true
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errorDevice",
        success: 'valid',
        messages: {
            b_ttable: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)",
                number: "请输入数字"
            },
            b_calcExpr: {
                required: " (必填字段)"
            },
            b_tcolumn: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)"
            },
            b_scolumn: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)"
            },
            b_calcDate: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)",
            },
            b_tcode: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)",
                number: "请输入数字"
            },

            b_stable: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)",
                number: "请输入数字"
            },
            b_scode: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)",
                number: "请输入数字"
            },

            b_calcStart: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",

                number: "请输入数字",

            },
            b_calcEnd: {
                required: " (必填字段)",
                minlength: " (不能少于 1 个字母)",
                number: "请输入数字"
            },
            b_status: {

                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",

                number: "请输入数字"

            },

            b_scale: {
                required: " (必填字段)",

                minlength: " (不能少于 1 个字母)",

                number: "请输入数字",

            },


            b_rowSort: {
                required: " (必填字段)",
                minlength: " (不能少于1位)",
                number: "请输入数字",
            }
        }
    });
}

var pageParams;

function returnHome() {
    $$.goUrl("/iot/calcreport/calc_list.html?rptCode=" + pageParams.rptCode + "&thirdMenuCode=RptManger");
}
$(function () {
    forwardCalcValid();
    pageParams = $$.getQueryString();
    $("#subBtn").on('click', function () {
        //alert("subbtn.click");
        addData();
    });

    $("#returnBut").click(function () {
        returnHome();
    });

    $("#b_ttable").val(pageParams.rptCode);


    $("input[name^='b_status']").click(function () {
        $("#b_status").val($(this).val());
    });
});

jQuery.validator.addMethod("isCode", function (value, element) {

    //校验编码是否存在
    var tcodeOld = $("#b_tcodeOld").val();
    var tcode = $("#b_tcode").val();
    if (tcodeOld == tcode) {
        return true;
    } else {

        var args = {};
        args.c_isRpt_1 = 1;
        args.c_ttable_1 = pageParams.rptCode;
        args.c_tcode_1 = $("#b_tcode").val();
        args.page = 0;
        args.pageSize = 5;
        args.isPage = 1;


        var exists;
        $$.ajax({
            url: "/bootstrap/basegrid/IotCalcBusiness/list",
            async: false,
            data: args,
            success: function (data) {
                if (data.length && data.length > 0) {
                    exists = true;
                } else {
                    exists = false;
                }
            }
        });
        if (exists) {
            $(element).data('error-msg', '编码不能重复！');
            return false;
        }
        return true;
    }

}, function (params, element) {
    return $(element).data('error-msg');
});